<!DOCTYPE html>
<html lang="en">
<head>
    <#include "../common/header.ftl">
    <script>
    	$(function(){
    		///提交表单, 全选自己角色下拉框
    		$("#btn_submit").click(function(){
    			//全选自己角色下拉框
    			$(".selfPermissions option").prop("selected", true);
    			//表单提交
    			$("#editForm").submit();
    		});

    		//1: 页面加载完毕之后, 将自己的权限[id]全部获取出来, 放置到一个数组中
    		//参数1:操作数组
    		//参数2:回调方法, 目的是后去遍历option数组中每个元素的value值
    		//结果: 自己角色id数组
    		var ids = $.map( $(".selfPermissions option"), function(option){
				  return option.value;
			});
    		//2:将系统的权限[id]也全部获取出来
    		var allPermissions = $(".allPermissions option");
    		
    		//遍历数组
    		//参数1:数组
    		//参数2:回调方法
    		$.each( allPermissions, function(index, item){
        		//3:遍历系统角色[id]一个个去跟 第一步获取到数组对比
        		///判断元素是否在数组中
        		//参数1:判断元素
        		//参数2:数组
        		//返回: 如果存在, 返回数组索引, 不存在返回-1
        		if($.inArray(item.value, ids) >= 0){
	    			//3.1: 如果存在, 删除当前比较系统角色
	    			$(item).remove();
        		}else{
        			//3.2: 如果不存在, 保留
        		}
    		});
    	});

    	//全部移动
    	function moveAll(srcCls, targetCls){
    		$("."+srcCls+" option").appendTo($("."+targetCls));
    	}

    	//选中移动
    	function moveSelected(srcCls, targetCls){
    		$("."+srcCls+" option:selected").appendTo($("."+targetCls));
    	}
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">角色编辑</h1>
                </div>
            </div>
            <div class="row col-sm-10">
                <form class="form-horizontal" action="/role/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" value="${entity.id}" name="id">
                    <div class="form-group" >
                        <label for="name" class="col-sm-2 control-label">角色名称：</label>
                        <div class="col-sm-6">
                        <input type="text" class="form-control" id="name" name="name" value="${entity.name}" placeholder="请输入角色名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-2 control-label">角色编码：</label>
                        <div class="col-sm-6">
                        <input type="text" class="form-control" id="sn" name="sn" value="${entity.sn}" placeholder="请输入角色编码">
                        </div>
                    </div>
                    <div class="form-group" id="role">
                        <div>
                            <label for="role" class="control-label" style="margin-left: 60px">权限：</label>
                        </div>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-4 col-sm-offset-1">
                                <select multiple class="form-control allPermissions" size="15">
                                   <#list permissions as p>
                                   	<option value="${p.id}">${p.name}</option>
                                   </#list>
                                </select>
                            </div>
                            <div class="col-sm-2" style="margin-top: 60px;" align="center">
                                <div >
                                    <a type="button" class="btn btn-info  "  style="margin-top: 10px"
                                       onclick="moveSelected('allPermissions', 'selfPermissions')">&nbsp;&gt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveSelected('selfPermissions', 'allPermissions')">&nbsp;&lt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('allPermissions', 'selfPermissions')">&gt;&gt;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('selfPermissions', 'allPermissions')">&lt;&lt;</a>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <select multiple class="form-control selfPermissions" size="15" name="ids">
                                    <#list entity.permissions as p>
                                   	<option value="${p.id}">${p.name}</option>
                                   </#list>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button id="btn_submit" type="button" class="btn btn-default">保存</button>
                            <button type="reset" class="btn btn-default">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>